<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <head>
        <p:resources />
    </head>
    <body>
        <ui:composition template="./../../Template/generalTemplate.xhtml">
            <ui:define name="content">


                <h:form id="form1" prependId="false" >  
                    <p:wizard showNavBar="false" showStepStatus="false"  widgetVar="wiz" flowListener="#{createNewSalesOrderManagedBean.onFlowProcess}">

                        <p:tab title="Sales Quotation Info">


                            <p:panel style="margin-top:100px; margin-left:20px; width:800px;" header="Sales Quotation Information">   

                                <h:outputText value="Company:" />  
                                <h:outputText style= " color: blue" value="#{createNewSalesOrderManagedBean.salesQuotation.company.name}" />

                                <p:dataTable var="lineItem" value="#{createNewSalesOrderManagedBean.salesQuotation.quotationLineItems}" dynamic="true" emptyMessage="No product selected">
                                    <p:column>
                                        <f:facet name="header">
                                            <h:outputText value="Proudct" />
                                        </f:facet>
                                        <h:outputText value="#{lineItem.product.productName}" />
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <h:outputText value="Quantity (in cases)" />
                                        </f:facet>
                                        <h:outputText value="#{lineItem.quantity}" />
                                    </p:column>
                                    <p:column>
                                        <f:facet name="header">
                                            <h:outputText value="Unit Price" />
                                        </f:facet>
                                        <h:outputText value="#{lineItem.price}" />
                                    </p:column>
                                </p:dataTable>
                                <br/>
                                <h:outputText value="User Discount"/><p:spacer width="10" />
                                <h:outputText value="#{createNewSalesOrderManagedBean.salesQuotation.userDiscount}"/>



                            </p:panel>

                            <p:commandButton style="margin-top:50px; margin-left:780px;" value="Next" onclick="wiz.next();" ></p:commandButton>



                        </p:tab>   
                        <p:tab title="Create Sales Order" id ="salesOrder">



                            <p:panel header="Create Sales Order" style="margin-top:30px; width: 900px; height:400px;">    
                                <p style="color:slategray; font:12px;">Sales Order LineItem List</p>
                                <p:dataTable  id ="lineItemList" dynamic="true" emptyMessage="No product selected" value="#{createNewSalesOrderManagedBean.salesOrderLineItemList}" var="o" >

                                    <p:column>
                                        <f:facet name="header">Product Name</f:facet>
                                        #{o.product.productName}
                                    </p:column> 

                                    <p:column>
                                        <f:facet name="header">Quantity</f:facet>
                                        #{o.quantity}
                                    </p:column>

                                    <p:column>
                                        <f:facet name="header">Unit Price</f:facet>
                                        #{o.price}
                                    </p:column>

                                    <p:column>
                                        <f:facet name="header">Cum Amount</f:facet>
                                        #{o.cumAmount}
                                    </p:column>

                                    <p:column>
                                        <f:facet name="header">Edit</f:facet>
                                        <p:commandLink value =" Delete" update ="lineItemList" ajax ="false" action="#{createNewSalesOrderManagedBean.deleteLineItem(o)}"/>
                                    </p:column>        
                                </p:dataTable>

                                <div  style="margin-top:50px;">
                                    <p:panel header="Add The LineItem" toggleable="true">
                                        <table>
                                            <td><h:selectOneMenu id="productType" value="#{createNewSalesOrderManagedBean.productId}">  
                                                    <f:selectItems value="#{createNewSalesOrderManagedBean.getProductListFromSalesQuotation()}" />
                                                </h:selectOneMenu>
                                                <p:message for="productType"/></td>

                                            <td><h:outputText value = "Quantity"/>
                                                <h:inputText id="a" value="#{createNewSalesOrderManagedBean.lineItem.quantity}" />
                                                <p:message for="a"/></td>

                                            <td><h:outputText value = "Unit Price"/></td>
                                            <td><font style="color:blue;"><h:outputText id="unitPrice" value="#{createNewSalesOrderManagedBean.lineItem.price}"/></font>
                                            </td>

                                            <td><h:outputText value = "Cum Amount"/></td>
                                            <td><font style="color:blue;"><h:outputText  id="cumAmount" value="#{createNewSalesOrderManagedBean.lineItem.cumAmount}" /></font>
                                            </td>
                                            <p:poll interval="2"   
                                                    actionListener="#{createNewSalesOrderManagedBean.calculateCumAmount}" update="unitPrice cumAmount" />
                                            <td><p:commandButton value="Add" update="lineItemList" ajax ="true" actionListener ="#{createNewSalesOrderManagedBean.addLineItem}"/></td>
                                        </table>

                                    </p:panel>

                                </div>

                                <div style="margin-top:20px;">
                                    <p:commandButton value ="Verify Price"  update="btm" actionListener="#{createNewSalesOrderManagedBean.verifyPrice}"/>

                                </div>  

                            </p:panel>

                            <p:commandButton style="margin-top:20px; position:relative; float:left;" value="Back" onclick="wiz.back();" ></p:commandButton>
                            <p:commandButton style="margin-top:20px; position:relative; margin-left:350px" value="Save Draft" actionListener="#{createNewSalesOrderManagedBean.saveDraft}"/>
                            <p:commandButton style="margin-top:20px; margin-left:350px; position:relative;" value="Next" onclick="wiz.next()" ></p:commandButton>






                        </p:tab>


                        <p:tab title="Generate Delivery Orders" id="deliveryOder">



                            <p:panel header="Generate Delivery Orders" style= "height:450px;overflow:auto;">


                                <p style="color:blue; font:18px;">
                                    <h:selectBooleanCheckbox value="#{createNewSalesOrderManagedBean.salesOrder.cashInAdvance}" />
                                    Cash In Advance
                                </p>



                                <p:dataTable  id="deliveryOrderList" dynamic="true" emptyMessage="No product selected" value="#{createNewSalesOrderManagedBean.deliveryOrderList}" var="deliveryOrder" >

                                    <p:column>
                                        <f:facet name="header">Delivery Order #</f:facet>
                                        #{deliveryOrder.id}
                                    </p:column>

                                    <p:column>
                                        <f:facet name="header">Line Item</f:facet>
                                        <p:commandLink update="datatable2" ajax="true" value="Show" onclick="dlg2.show()">
                                            <f:setPropertyActionListener value="#{deliveryOrder}" target="#{createNewSalesOrderManagedBean.selectedDeliveryOrder}"/>
                                        </p:commandLink>   
                                        <p:dialog header="DelvieryOrder LineItemList" widgetVar="dlg2" resizable="true" width="500"> 
                                            <h:panelGrid id= "display">
                                                <p:dataTable id ="datatable2" value="#{createNewSalesOrderManagedBean.selectedDeliveryOrder.deliveryOrderLineItems}" var="lineItem">
                                                    <p:column>
                                                        <f:facet name="header">Product</f:facet>
                                                        #{lineItem.product.productName}
                                                    </p:column>  
                                                    <p:column>
                                                        <f:facet name="header">Quantity</f:facet>
                                                        #{lineItem.quantity}
                                                    </p:column>  
                                                    <p:column>
                                                        <f:facet name="header">Unit Price</f:facet>
                                                        #{lineItem.price}
                                                    </p:column>  
                                                    <p:column>
                                                        <f:facet name="header">Total Price</f:facet>
                                                        #{lineItem.cumAmount}
                                                    </p:column>  

                                                </p:dataTable>  
                                            </h:panelGrid>
                                        </p:dialog>  

                                    </p:column>    

                                    <p:column>
                                        <f:facet name="header">Delivery Order Date</f:facet>
                                        <h:outputText value="#{deliveryOrder.deliveryDate}">
                                            <f:convertDateTime pattern ="yyyy-MM-dd"/>
                                        </h:outputText>    
                                    </p:column>

                                    <p:column>
                                        <f:facet name="header">Delivery Location</f:facet>
                                        <h:outputText value="#{deliveryOrder.shippingAddress.streetName},"/>
                                        <h:outputText value="#{deliveryOrder.shippingAddress.city}"/>

                                    </p:column> 

                                    <p:column>
                                        <f:facet name="header">Edit</f:facet>
                                        <p:commandLink value =" Delete" update="deliveryOrderList" ajax ="false" action="#{createNewSalesOrderManagedBean.deleteDeliveryrder(deliveryOrder)}"/>
                                    </p:column> 


                                </p:dataTable>

                                <div id="subDiv" style="margin-top:30px;">
                                    <p:panel header="Create Delivery Order">
                                        <div id="subDiv1" style="margin-top:15px;">   
                                            <p:dataTable  id ="deOrderLineitemList" dynamic="true" emptyMessage="No product selected" value="#{createNewSalesOrderManagedBean.deliveryOrderLineItemList}" var="a" >

                                                <p:column>
                                                    <f:facet name="header">Product Name</f:facet>
                                                    #{a.product.productName}
                                                </p:column> 

                                                <p:column>
                                                    <f:facet name="header">Quantity</f:facet>
                                                    #{a.quantity}
                                                </p:column>

                                                <p:column>
                                                    <f:facet name="header">Unit Price</f:facet>
                                                    #{a.price}
                                                </p:column>

                                                <p:column>
                                                    <f:facet name="header">Cum Amount</f:facet>
                                                    #{a.cumAmount}
                                                </p:column>

                                                <p:column>
                                                    <f:facet name="header">Edit</f:facet>
                                                    <p:commandLink value =" Delete" ajax ="false" update="deOrderLineitemList" action="#{createNewSalesOrderManagedBean.deleteLineItem_deliveryOrder(a)}"/>
                                                </p:column>        
                                            </p:dataTable> 
                                        </div>


                                        <div style="margin-top:15px;"> 
                                            <table> 
                                                <td width="240"><h:selectOneMenu id="productType_deliveryOrder" value="#{createNewSalesOrderManagedBean.productId_dOrder}" >  
                                                        <f:selectItems value="#{createNewSalesOrderManagedBean.getProductListFromSalesOrder()}" />
                                                    </h:selectOneMenu></td>
                                                <td><p:message for="productType_deliveryOrder"/></td>

                                                <td width="240"> <h:outputText value = "Quantity"/>
                                                    <h:inputText id="a2" value="#{createNewSalesOrderManagedBean.lineItem.quantity}" /></td>


                                                <td width="150"> <h:outputText value = "Unit Price"/>
                                                    <font style="color:blue;"><h:outputText id="unitPrice_deliveryOrder" value="#{createNewSalesOrderManagedBean.lineItem.price}"/></font></td>
                                                <td width="150"> <h:outputText value = "Amount Left"/>
                                                    <font style="color:blue;"><h:outputText  id="amountLeft_deliveryOrder" value="#{createNewSalesOrderManagedBean.amountLeft}" /></font></td>


                                                <td width="200"> <h:outputText value = "Cum Amount"/>
                                                    <font style="color:blue;"><h:outputText  id="cumAmount_deliveryOrder" value="#{createNewSalesOrderManagedBean.lineItem.cumAmount}" /></font></td>



                                                <td width="150"><p:commandLink value="Add LineItem" update="btm deOrderLineitemList" ajax ="true" action ="#{createNewSalesOrderManagedBean.addLineItemToDeliveryOrder}" />  </td>

                                                <p:poll interval="2"   
                                                        actionListener="#{createNewSalesOrderManagedBean.calculateCumAmountForDeliveryOrder}" update="unitPrice_deliveryOrder cumAmount_deliveryOrder"  />

                                                <p:poll interval="2"   
                                                        actionListener="#{createNewSalesOrderManagedBean.calculateAmountLeft}" update="amountLeft_deliveryOrder"  /> 




                                            </table>                                                 
                                        </div>

                                        <div style="margin-top:15px;">
                                            <table>
                                                <tr>
                                                    <td><h:outputText value = "Delivery Date"/></td>
                                                    <td><p:calendar value="#{createNewSalesOrderManagedBean.deliveryOrder.deliveryDate}" showOn="button" pattern="yyyy-MM-dd"/></td>
                                                </tr>
                                                <tr>
                                                    <td><h:outputText value = "Delivery Location"/></td>
                                                    <td><h:selectOneMenu id="deliveryLocation" value="#{createNewSalesOrderManagedBean.deliveryLocationId}">  
                                                            <f:selectItems value="#{createNewSalesOrderManagedBean.getShippingAddressList()}" />
                                                        </h:selectOneMenu>
                                                        <p:message for="deliveryLocation"/></td>
                                                </tr>
                                                <tr>
                                                    <td><h:outputText value ="Billing Address"/></td>
                                                    <td><h:selectOneMenu id="billingAddress" value="#{createNewSalesOrderManagedBean.billingAddressId}">  
                                                            <f:selectItems value="#{createNewSalesOrderManagedBean.getBillingAddressList()}" />
                                                        </h:selectOneMenu>
                                                        <p:message for="billingAddress"/></td>
                                                </tr>
                                            </table>    

                                        </div>
                                        <p:commandButton value="Create Delivery Order" update="deliveryOrderList deOrderLineitemList btm" ajax="false" actionListener ="#{createNewSalesOrderManagedBean.addDeliveryOrder}"/>
                                    </p:panel>

                                </div>

                            </p:panel>




                            <p:commandButton style ="margin-top:10px; margin-left:0px; position:relative;float:left;" value="Back" onclick="wiz.back();"></p:commandButton>
                            <p:commandButton style="margin-top:10px; position:relative; margin-left:425px" value="Save Draft" actionListener="#{createNewSalesOrderManagedBean.saveDraft}"/>
                            <p:commandButton style="margin-top:10px; margin-left:425px; position:relative;" value="Create" update="btm" ajax="true" actionListener ="#{createNewSalesOrderManagedBean.createSalesOrder}" ></p:commandButton>

                        </p:tab> 


                    </p:wizard> 

                </h:form>

            </ui:define>

            <ui:define name="bottom">
                <h:form id="btm">
                    <p:messages id = "msg2"/>
                </h:form>
            </ui:define>

        </ui:composition>

    </body>
</html>
